import { IColumns, StoreFuncs } from "../../ExcelStore";
import { get } from 'lodash';
import { dotPngImg, getInputValue, getHreflink, linkedColor } from "./FieldCellType";
import $ from 'jquery';

export default (column: IColumns, editable: boolean) => {
    if (editable) {
        class Type extends GC.Spread.Sheets.CellTypes.ComboBox {
            constructor(options) {
                super();
                this.items(options.map(d => ({ value: d.value, text: d.name })));
                this.editorValueType(GC.Spread.Sheets.CellTypes.EditorValueType.value);
            }
            paint(ctx: CanvasRenderingContext2D, value: any, x: number, y: number, w: number, h: number, style: GC.Spread.Sheets.Style, context?: any) {
                try {
                    ctx.clearRect(x, y, w, h);
                    super.paint(ctx, getInputValue(value), x, y, w, h, style, context);
                    const { sheet, row, col } = context;
                    if (get(sheet.modifyedValues, `${StoreFuncs.getRandomId(row)}.${col}`)) {
                        ctx.drawImage(dotPngImg, x, y, 8, 8);
                    }
                } catch (e) {
                }
            }
            getAutoFitWidth(value: any, text: string, cellStyle: GC.Spread.Sheets.Style, zoomFactor: number, context?: any): number {
                return super.getAutoFitWidth(getInputValue(value), text, cellStyle, zoomFactor, context);
            }
            setEditorValue(editorContext: any, value: any, context?: any): void {
                const inputValue = getInputValue(value);
                return super.setEditorValue(editorContext, inputValue, context)
            }
            getEditorValue(editorContext: any, context?: any): any {
                const { sheet, row, col } = context;
                const value = sheet.getValue(row, col);
                const hreflink = getHreflink(value);
                let inputValue = super.getEditorValue(editorContext, context);

                return JSON.stringify({
                    hreflink,
                    value: inputValue
                })

            }
        }
        return Type;
    } else {
        class Type extends GC.Spread.Sheets.CellTypes.Base {
            options = {};
            itemWidth = {};
            constructor(options) {
                super();
                options.forEach(v => {
                    this.options[v.value] = v.name;
                })
            }
            paint(ctx: CanvasRenderingContext2D, value: any, x: number, y: number, w: number, h: number, style: GC.Spread.Sheets.Style, context?: any) {
                try {
                    ctx.clearRect(x, y, w, h);
                    if (!editable && getHreflink(value)) {
                        style.foreColor = linkedColor;
                        style.textDecoration = GC.Spread.Sheets.TextDecorationType.underline;
                    }
                    const { row } = context;
                    this.itemWidth[row] = parseInt(ctx.measureText(getInputValue(value)).width.toString());
                    return super.paint(ctx, value, x, y, w, h, style, context);
                } catch (e) {
                }
            }
            getAutoFitWidth(value: any, text: string, cellStyle: GC.Spread.Sheets.Style, zoomFactor: number, context?: any): number {
                return super.getAutoFitWidth(value, text, cellStyle, zoomFactor, context);
            }
            format(value: any, format: any, formattedData?: any, context?: any) {
                value = getInputValue(value);
                return value && value.split(',').map(v => this.options[v]).join(',')
            }
            processMouseUp(hitInfo: GC.Spread.Sheets.IHitTestCellTypeHitInfo): boolean {
                const { sheet, row, col, sheetArea, isReservedLocation } = hitInfo as any;
                if(isReservedLocation) {
                    const hreflink = getHreflink(sheet.getValue(row, col, sheetArea));
                    if (hreflink) {
                        (window as any).cubeOpenHreflinkFromExcel(hreflink);
                    }
                }
                return true;
            }
            processMouseMove(hitInfo: GC.Spread.Sheets.IHitTestCellTypeHitInfo) {
                const { sheet, isReservedLocation } = hitInfo as any;
                var div = sheet.getParent().getHost();
                var canvasId = div.id + "vp_vp";
    
                var canvas = $("#" + canvasId)[0];
                if (sheet && isReservedLocation) {
                    canvas.style.cursor = 'pointer';
                    return true;
                } else {
                    canvas.title = "";
                    canvas.style.cursor = 'default';
                }
                return false;
            };
            getHitInfo(x: number, y: number, cellStyle: GC.Spread.Sheets.Style, cellRect: GC.Spread.Sheets.Rect, context?: any): GC.Spread.Sheets.IHitTestCellTypeHitInfo {
                const { row, col, sheetArea, sheet } = context;
                const info = {
                    x,
                    y,
                    row,
                    col,
                    cellRect,
                    sheetArea,
                    sheet,
                    isReservedLocation: x - cellRect.x <= this.itemWidth[row]
                } as any;
                return info;
            }
        }
        return Type;
    }
}